// 封装wx.request网络模块
module.exports = (url, methods, datas) => {
  let p = new Promise((resolve, reject) => {
    let type = 'application/json';
    let data = datas;
    let method = methods;

    wx.request({
      url,
      method,
      data,
      header: {
        token: wx.getStorageSync('token') || '', // 登录后，后端返回的token值
        'content-type': type
      },
      success(res) {
        const data = res.data;
        if(data === 'login required') {
          wx.removeStorageSync('token');
          wx.removeStorageSync('userInfo');
          wx.showModal({
            title: '温馨提示',
            content: '当前尚未登录，请先登录',
            success: function(res) {
                return wx.switchTab({
                  url: '../login/login', // 点击确定去登陆
                })
            }
          });
        }
        resolve(res);
      },
      fail(err) {
        reject(err);
      }
    })
  });
  return p;
}